package com.shenfa.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.shenfa.bean.Paper;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.bean.PaperInfo;
import com.shenfa.bean.Person;
import com.shenfa.bean.TreeNodeCheck;
import com.shenfa.service.PaperService;
import com.shenfa.tools.SteelDate;

import com.shenfa.util.ResultObject;

/**
 * 预印纸管理
 * 
 * @author longchensan create by 2011-8-28
 * 
 */
public class PaperAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	// service
	@Autowired
	private PaperService paperService;
	
	private PaperCityCrd paperCC;

	private static Logger log=Logger.getLogger(PaperAction.class);
	// 返回结果集对象
	public JSONObject result;
	// 分页，第几条数据
	private int start;
	// 每页显示数据条数
	private int limit;
	// 接收页面数据
	private Paper paper;//预印纸
	
	private PaperInfo paperInfo;//预印纸从表

	private HttpServletRequest request;
	
	//获取预印纸序列号
	public String nextKey(){
		String key = paperService.nextKey();
		result = ResultObject.build("0", key);
		return SUCCESS;
	}
	
	//验证预印纸编号是否存在 0-不存在，1-存在
	public String checkPaperNo(){
		String flag = paperService.checkPaperNo(paperCC.getPaper_no());
		result = ResultObject.build(flag, null);
		return SUCCESS;
	}
	
	/**
	 * 根据有效期时段和卡产品id查询预印纸
	 * @return
	 */
	public String paperByTimeCard() {
		log.debug(paper.toString());
		List<Paper> list = new ArrayList<Paper>();
		list = paperService.doPaperByTimeCard(paper);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	
	/**
	 * 生成页面gridpanel数据
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String GridList() {
		log.debug("start="+start+",limit="+limit+",cardNo="+paperCC.getCard_no());
		List<Paper> list = paperService.doGridList(paperCC.getCard_no(), start, limit);
		result = ResultObject.build("0", list);
		result.put("total", paperService.queryCount(paperCC.getCard_no()));
		return SUCCESS;
	}

	/**
	 * 对应底板信息的预印纸从表信息grid数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String paperInfoList() {
		log.debug("cardNo="+paperCC.getCard_no());
		List<PaperInfo> list = paperService.doPaperInfoLsit(paperCC.getPaper_no());
		result = ResultObject.build("0", list);
		result.put("total", list.size());
		return SUCCESS;
	}
	
	/**
	 * 根据预印纸id和底板编号查询单条从表信息
	 * @return
	 */
	public String paperInfo() {
		log.debug(paperInfo.toString());
		List<PaperInfo> list = paperService.doAPaperInfo(paperInfo);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	/**
	 * 新增时查询卡产品id外包商对应的未绑定预印纸的城市集合
	 * @return
	 */
	public String cityByCardList() {
		log.debug(paperCC.toString());
		List<TreeNodeCheck> list = paperService.cityByCardList(paperCC);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	
	/**
	 * 修改时查询卡产品id、预印纸id对应的城市集合
	 * @return
	 */
	public String cityByCardPrintList() {
		log.debug(paperCC.toString());
		List<TreeNodeCheck> list = paperService.cityByCardPrintList(paperCC);
		result = ResultObject.build("0", list);
		return SUCCESS;
	} 
	
	/**
	 * 添加预印纸
	 * @return
	 */
	public String addPaper() {
		log.debug(paper.toString());
		log.debug(paperCC.toString());
		request = ServletActionContext.getRequest();
		Person currentPerson=(Person) request.getSession().getAttribute("login");
		paper.setPerson_no(currentPerson.getId());
		paper.setCreate_time(SteelDate.getDate(SteelDate.YYYY_MM_DD_HH_MM_SS, new Date()));
		int type = paperService.addP(paper, paperCC.getCard_no(), paperCC);
		if(type==0){
			result = ResultObject.build(String.valueOf(type), "添加成功!");
		}else{
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}
	
	/**
	 * 更新预印纸
	 * @return
	 */
	public String updPaper() {
		log.debug(paper.toString());
		log.debug(paperCC.toString());
		int type = paperService.udpP(paper,paperCC);		
		if(type==0){
			result = ResultObject.build(String.valueOf(type), "修改成功!");
		}else{
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}
	
	/**
	 * 解除预印纸与外包商关联关系
	 * @return
	 */
	public String delPaper() {
		log.debug(paperCC.toString());
		int type = paperService.delP(paperCC);
		
		if(type==0){
			result = ResultObject.build("0", "删除成功!");
		}else{
			result = ResultObject.build("1", "操作失败!");
		}
		return SUCCESS;
	}
	/**
	 * 添加预印纸信息从表
	 * @return
	 */
	public String addPaperInfo() {
		log.debug(paperInfo.toString());
		int type = paperService.addPInfo(paperInfo);
		
		if(type==0){
			result = ResultObject.build(String.valueOf(type), "添加成功!");
		}else{
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}
	/**
	 * 修改预印纸信息从表
	 * @return
	 */
	public String updPaperInfo() {
		log.debug(paperInfo.toString());
		int type = paperService.udpPInfo(paperInfo);
		
		if(type==0){
			result = ResultObject.build(String.valueOf(type), "修改成功!");
		}else{
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}
	/**
	 * 删除预印纸信息从表
	 * @return
	 */
	public String delPaperInfo() {
		log.debug(paperInfo.toString());
		int type = paperService.delPInfo(paperInfo);
		
		if(type==0){
			result = ResultObject.build(String.valueOf(type), "删除成功!");
		}else{
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}
	
	/**
	 * 根据预印纸ID删除从表信息
	 */
	public String delPaperInfoById(){
		log.debug(paper.toString());
		int type = paperService.delPaperInfoById(paper.getPid());
		if(type==0){
			result = ResultObject.build("0", "删除成功!");
		}else{
			result = ResultObject.build("1", "操作失败!");
		}
		return null;
	}

	/**
	 * 分页处理，起始条数
	 * 
	 * @param start
	 */
	public void setStart(int start) {
		this.start = Math.abs(start);
	}

	public int getStart() {
		return start;
	}

	/**
	 * 分页处理，每页数据条数
	 * 
	 * @param limit
	 */
	public void setLimit(int limit) {
		this.limit = Math.abs(limit);
	}

	public int getLimit() {
		return limit;
	}

	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public PaperCityCrd getPaperCC() {
		return paperCC;
	}

	public void setPaperCC(PaperCityCrd paperCC) {
		this.paperCC = paperCC;
	}

	public PaperInfo getPaperInfo() {
		return paperInfo;
	}

	public void setPaperInfo(PaperInfo paperInfo) {
		this.paperInfo = paperInfo;
	}

}
